<template>
	<view>
		<view class="search-box">
			<uni-search-bar @input="input" :radius="100" 
			cancelButton="none" :value="kw?kw:''" @confirm="search"></uni-search-bar>
		</view>
		<view class="history-box">
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size='17' @click="clear"></uni-icons>
			</view>
			<view class="history-list" v-for="(item,i) in historys" :key="i" @click="gotoDetail1(item)">
				<view class="thumb">
					<img src="/static/logo1.png" alt="" />
				</view>
				<view class="info">
					<text class="history-title2">{{item}}</text>
					<text class="answer">{{answerList[i]}}</text>
					
				</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
	</view>
	
	
</template>

<script>
import { computed } from "vue";
	export default {
		data() {
			return {
				timer:null,
				kw:'',
				historyList:[],
				answer:[],
				count:0
			};
		},
		onLoad() {
			console.log(this.historyList)
			console.log(this.answer)
			this.historyList=JSON.parse(uni.getStorageSync('kw')||'[]')
			this.answer=JSON.parse(uni.getStorageSync('ans')||'[]')
		},
		onPullDownRefresh(){
			
			this.kw=''
			// this.count=0
			setTimeout(function () {
						uni.stopPullDownRefresh();
					}, 500);
		},
		methods:{
			// 历史记录自动填充
			fill(item){
				this.kw=item
			},
			// 刚刚输入时触发
			input(e){
				clearTimeout(this.timer)
				this.timer=setTimeout(()=>{
					this.kw=e
					if(this.kw==='')return
				},500)
			},
			search(){
				this.saveSearchHistory()
				this.save()
				this.gotoDetail2()
			},
			// 当点击项目时，跳转到详情页
			gotoDetail1(item){
				uni.navigateTo({
					url:'/pages/detail/detail?kw='+item
				})
			},
			gotoDetail2(){
				uni.navigateTo({
					url:'/pages/detail/detail?kw='+this.kw
				})
			},
			//搜索之后保存历史记录到本地
			saveSearchHistory(){
				this.count=this.historyList.length
				const set = new Set(this.historyList)
				// set.delete(this.kw)
				set.add(this.kw)
				this.historyList=Array.from(set)
				var _this=this
				uni.request({
					url:'http://47.97.5.12:5000//api//data',
					method:"POST",
					header:{
					"Content-Type": "application/x-www-form-urlencoded"	
					},
					data:{
						input:JSON.stringify(this.kw)
					},
					success:function(res){
						if(res.statusCode===200){
							if(_this.count!==_this.historyList.length){
								_this.answer.push(res.data.output)
								_this.save()
							}
						}
					},
					fail: (err) => {
						console.log(err)
					}
				})
				console.log(this.answer)
				console.log(this.historyList)
				uni.setStorageSync('kw',JSON.stringify(this.historyList))
			},
			//点击清空按钮之后，清空聊天记录
			clear(){
				this.historyList=[]
				uni.setStorageSync('kw','[]')
				this.answer=[]
				uni.setStorageSync('ans','[]')
			},
			save(){
				// 存储数据，保存到本地
				uni.setStorageSync('ans',JSON.stringify(this.answer))
			}
		},
		
		computed:{
			historys(){
				return [...this.historyList].reverse()
			},
			answerList(){
				return [...this.answer].reverse()
			}
		}
	}
</script>

<style lang="scss">
.search-box{
	position: sticky;
	top:0;
	z-index: 999;
}
.history-box{
	padding: 0 5px;
	.history-title{
		display: flex;
		justify-content: space-between;
		height:40px;
		align-items: center;
		font-size: 13px;
		border-bottom: 1px solid #efefef;
	}
	.history-list{
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		height:80px;
		border-bottom: 1px solid #efefef;
		box-shadow: 0.5px 0.5px 7.5px #ddd;
		.thumb img{
			display: block;
			margin-right:7.5px;
			margin-left: 5px;
			margin-top: 5px;
			width: 75px;
			height: 75px;
			border-radius: 15px;
		}
		.info{
			position:absolute;
			left:90px;
			display: flex;
			flex-direction: column;
			justify-content: space-around;
			height: 80px;
			.answer{
				width:250px;
				font-size: 10px;
				-webkit-line-clamp: 2;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				overflow: hidden;
				text-overflow: ellipsis;
			}
			.history-title2{
				width:250px;
				font-size: 18px;
				font-weight: bold;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}
		}
		.uni-tag{
			margin-top: 5px;
			margin-right: 5px;
		}
	}
}
</style>
